﻿using System;
using System.Linq;
using PumpStationClient.PumpService;

namespace PumpStationClient
{
    public class PumpStation
    {
        private int _waterlevel;

        public int Id { get; private set; }
        public string IPAddress { get; private set; }
        public int Port { get; private set; }

        public Pump[] Pumps { get; set; }
        public PumpStationServiceClient Proxy { get; set; }

        public int Waterlevel
        {
            get
            {
                return _waterlevel;
            }
            set
            {
                if (value >= 0 && value <= 3)
                {
                    _waterlevel = value;
                    Proxy.UpdateWaterlevel(Id, value);
                }
            }
        }

        public PumpStation(string ipAddress, int port, PumpStationServiceClient proxy)
        {
            IPAddress = ipAddress;
            Port = port;
            Proxy = proxy;
        }

        public void Register()
        {
            Console.Write("Registering with service... ");
            Id = Proxy.RegisterPumpStation(IPAddress, Port, Pumps.Select(pump => new PumpDTO
            {
                Capacity = pump.Capacity,
                Running = pump.Running
            }).ToArray());
            Console.WriteLine("[Complete]");
            Console.WriteLine("Registered as ID: " + Id);
        }
    }
}
